#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "creusator.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_ui(new Ui::MainWindow)
{
    m_ui->setupUi(this);
    this->m_ui->menubar->hide();
    goToPageMenu();
}

MainWindow::~MainWindow()
{
    delete m_ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::printDialogBuilding()
{
    DialogBuilding *d = new DialogBuilding();
    d->exec();
    delete d;
}

void MainWindow::printDialogOptions()
{
    /*
    Options *d = new Options();
    QObject::connect(d, SIGNAL(changeFullScreen(bool)), this, SLOT(setOptionPleinEcran(bool)));
    d->exec();
    delete d;
    */
}

void MainWindow::goToPage(QWidget *page)
{
    if (m_ui->stackedWidget->currentWidget() == m_ui->pageMainWindow)
        m_ui->creusator->pauseGame();

    oldPageIndex = m_ui->stackedWidget->currentIndex();
    m_ui->stackedWidget->setCurrentIndex(m_ui->stackedWidget->indexOf(page));
}

void MainWindow::goToOldPage()
{
    m_ui->stackedWidget->setCurrentIndex(oldPageIndex);

    if (m_ui->stackedWidget->currentWidget() == m_ui->pageMainWindow)
        this->m_ui->menubar->show();
    else
        this->m_ui->menubar->hide();
}

void MainWindow::goToPageMainWindow()
{
    goToPage(m_ui->pageMainWindow);
    m_ui->menubar->show();
    resize(m_ui->creusator->getNormalWidth(), m_ui->creusator->getNormalHeight());
}

void MainWindow::goToPageMenu()
{
    goToPage(m_ui->pageMenu);
    m_ui->menubar->hide();
}

void MainWindow::goToPageChooseLevel()
{
    goToPage(m_ui->pageChooseLevel);
    m_ui->menubar->hide();

    m_ui->listWidgetNew->clear();
    m_ui->listWidgetNew->addItems(m_ui->creusator->getLevelsList());
}

void MainWindow::goToPageLoad()
{
    goToPage(m_ui->pageLoad);
    m_ui->menubar->hide();

    m_ui->listWidgetLoad->clear();
    m_ui->listWidgetLoad->addItems(m_ui->creusator->getSavedGamesList());
}

void MainWindow::goToPageSave()
{
    goToPage(m_ui->pageSave);
    m_ui->menubar->hide();

    m_ui->listWidgetSave->clear();
    m_ui->listWidgetSave->addItem("Nouvelle sauvegarde");
    m_ui->listWidgetSave->addItems(m_ui->creusator->getSavedGamesList());
}

void MainWindow::setOptionPleinEcran(bool active)
{
    if (active) this->showFullScreen();
    else this->showNormal();

}

void MainWindow::on_buttonMenuNewGame_clicked()
{
    goToPageChooseLevel();
}



void MainWindow::on_actionSauvegarderPartie_triggered()
{
    goToPageSave();
}

void MainWindow::on_actionChargerPartie_triggered()
{
    goToPageLoad();
}

void MainWindow::on_actionRetourAuMenu_triggered()
{
    if(displayEndGameConfirmation())
    {
        m_ui->creusator->endGame();
        goToPageMenu();
    }
}


void MainWindow::on_pushButtonLoadGame_clicked()
{
    goToPageLoad();
}

void MainWindow::on_actionNouveauPartie_triggered()
{
    goToPageChooseLevel();
}

void MainWindow::on_pushButtonChooseLevel_clicked()
{
    if(m_ui->listWidgetNew->selectedItems().isEmpty())
        return;

    QStringList list;
    foreach(QListWidgetItem *item, m_ui->listWidgetNew->selectedItems())
        list << item->text();

    m_ui->creusator->newGame(list);
    goToPageMainWindow();
}

void MainWindow::on_pushButtonChooseGame_clicked()
{
    if(m_ui->listWidgetLoad->selectedItems().isEmpty())
        return;

    m_ui->creusator->loadGame(m_ui->listWidgetLoad->currentRow());

    goToPageMainWindow();
}



void MainWindow::on_pushButtonSaveGame_clicked()
{
    if(m_ui->listWidgetSave->selectedItems().isEmpty())
        return;

    bool ok;
    QString text = QInputDialog::getText(this, "Choisir une nom de sauvegarde",
                                         "Nom de la sauvegarde :",
                                         QLineEdit::Normal, "",  &ok);
    if(!ok || text.isEmpty()) return;

    m_ui->creusator->saveGame(text, m_ui->listWidgetSave->currentRow() - 1);
    goToOldPage();
}

void MainWindow::on_pushButtondeleteSave_clicked()
{
    if(m_ui->listWidgetSave->selectedItems().count() == 0) return;
    int pos = m_ui->listWidgetSave->currentRow();
    if(pos == 0) return;
    if(!displayDeleteSaveConfirmation()) return;

    m_ui->creusator->removeSavedGame(pos - 1);
    delete m_ui->listWidgetSave->item(pos);
}

void MainWindow::on_pushButtonDeleteLoad_clicked()
{
    if(m_ui->listWidgetLoad->selectedItems().count() == 0) return;
    int pos = m_ui->listWidgetLoad->currentRow();
    if(!displayDeleteSaveConfirmation()) return;


    m_ui->creusator->removeSavedGame(pos);
    delete m_ui->listWidgetLoad->item(pos);
}


bool MainWindow::displayDeleteSaveConfirmation()
{
    QMessageBox mb( "Supprimer une sauvegarde",
    "Etes-vous sur de vouloir supprimer cette sauvegarde ?",
    QMessageBox::Question,
    QMessageBox::Yes | QMessageBox::Default,
    QMessageBox::No, NULL);
    mb.setButtonText( QMessageBox::Yes, "Oui" );
    mb.setButtonText( QMessageBox::No, "Non" );

    return (mb.exec() == QMessageBox::Yes);
}


bool MainWindow::displayEndGameConfirmation()
{
    QMessageBox mb( "Quitter la partie",
    "Etes-vous sur de vouloir quitter la partie sans sauvegarder ?",
    QMessageBox::Question,
    QMessageBox::Yes | QMessageBox::Default,
    QMessageBox::No, NULL);
    mb.setButtonText( QMessageBox::Yes, "Oui" );
    mb.setButtonText( QMessageBox::No, "Non" );

    return (mb.exec() == QMessageBox::Yes);
}
